package com.googlecode.stemwijzer.client.rest;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.googlecode.stemwijzer.client.StemwijzerServiceAsync;
import com.googlecode.stemwijzer.shared.model.IsAnswer;
import com.googlecode.stemwijzer.shared.model.IsQuestion;
import com.googlecode.stemwijzer.shared.model.IsQuestionnaireResult;
import com.googlecode.stemwijzer.shared.serializer.IsAnswerSerializer;
import com.googlecode.stemwijzer.shared.serializer.IsQuestionSerializer;
import com.googlecode.stemwijzer.shared.serializer.IsQuestionnaireResultSerializer;
import com.googlecode.stemwijzer.shared.serializer.rest.json.AbstractJsonCodec;

/**
 * The Class RestStemwijzerServiceProxy.
 * 
 * @author jeroen.brosens
 * @author pascal janssen
 */
public class RestStemwijzerServiceProxy extends AbstractRestService implements StemwijzerServiceAsync {

	private static final String SERVICE_URL = "http://127.0.0.1:8080/stemwijzer-restservice-1.0-SNAPSHOT";

	@Override
	public void getResult(final AsyncCallback<IsQuestionnaireResult> callback) {

		jsonpRequest(SERVICE_URL + "/public/result", null, new AsyncCallback<String>() {

			@Override
			public void onSuccess(final String json) {
				IsQuestionnaireResultSerializer serializer = GWT.create(IsQuestionnaireResultSerializer.class);
				IsQuestionnaireResult result = serializer.fromJson(json);

				callback.onSuccess(result);
			}

			@Override
			public void onFailure(Throwable t) {
				callback.onFailure(t);
			}
		});
	}

	@Override
	public void registerUser(String userName, final AsyncCallback<Boolean> callback) {

		jsonpRequest(SERVICE_URL + "/public/register/" + userName, null, new AsyncCallback<String>() {

			@Override
			public void onSuccess(final String json) {
				Boolean result = parseBoolean(json);

				callback.onSuccess(result);
			}

			@Override
			public void onFailure(Throwable t) {
				callback.onFailure(t);
			}
		});
	}

	@Override
	public void resetSession(final AsyncCallback<Boolean> callback) {

		jsonpRequest(SERVICE_URL + "/public/resetsession", null, new AsyncCallback<String>() {

			@Override
			public void onSuccess(final String json) {
				Boolean result = parseBoolean(json);

				callback.onSuccess(result);
			}

			@Override
			public void onFailure(Throwable t) {
				callback.onFailure(t);
			}
		});
	}

	@Override
	public void sendAnswer(final IsQuestion question, final IsAnswer answer, final AsyncCallback<Boolean> callback) {

		Map<String, String> queryParams = new HashMap<String, String>();

		// serialize question
		final IsQuestionSerializer questionSerializer = GWT.create(IsQuestionSerializer.class);
		queryParams.put("question", questionSerializer.toJson(question));

		// serialize answer
		final IsAnswerSerializer answerSerializer = GWT.create(IsAnswerSerializer.class);
		queryParams.put("answer", answerSerializer.toJson(answer));

		jsonpRequest(SERVICE_URL + "/public/sendanswer", queryParams, new AsyncCallback<String>() {

			@Override
			public void onSuccess(final String json) {
				Boolean result = parseBoolean(json);

				callback.onSuccess(result);
			}

			@Override
			public void onFailure(Throwable t) {
				callback.onFailure(t);
			}
		});
	}

	private static Boolean parseBoolean(final String json) {
		AbstractJsonCodec<Boolean> codec = AbstractJsonCodec.BOOLEAN;
		Boolean result = codec.decode(JSONParser.parse(json));

		return result;
	}
}
